<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\index\controller;

//加载model
use app\index\model\UserModel;

class Chat extends CommonController {

    public $user_model;

    //put your code here
    function __construct() {
        parent::__construct();
        $this->user_model = new UserModel();
    }

    //聊天主页面
    function index() {
        //userinfo 
        $user_tokn = cookie('user_token');
        $userinfo = $this->user_info($user_tokn);
        if (empty($userinfo)) {
            $this->error('请先登录');
        }
        //个人信息
        $user_mine['username'] = $userinfo['nick_name'];
        $user_mine['id'] = $userinfo['id'];
        $user_mine['status'] = $userinfo['nick_name'];
        $user_mine['remark'] = $userinfo['remark'];
        $user_mine['avatar'] = $userinfo['header'];
        $this->assign('user_mine', json_encode($user_mine));
        //好友分组
        $frends_list = $this->user_frends_list($userinfo['id']);
        $this->assign('frends_list', $frends_list);
        //用户群
        $group_list = $this->user_group($userinfo['id']);
        $this->assign('group_list', $group_list);
        //
        $this->assign('user_tokn', $user_tokn);
        $this->assign('time', time());
        return view();
    }

    //用户加入的群列表
    function user_group($uid) {
        $user_group = $this->user_model->group_list($uid);
        if (empty($user_group)) {
            return '[]';
        }
        return json_encode($user_group);
    }

    //好友列表
    public function user_frends_list($uid) {
        //用户好友列表  好友分组
        $friends = $this->user_model->friends_list($uid);
        if (empty($friends)) {
            return '[]';
        }
        $friends_arr = array();
        //按用户分组 进行集合
        foreach ($friends as $key => $value) {
            $friends_arr[$value['friends_group']][$value['friends_id']]['username'] = $value['note_nickname'] ? $value['note_nickname'] : $value['nick_name'];
            $friends_arr[$value['friends_group']][$value['friends_id']]['id'] = $value['friends_id'];
            $friends_arr[$value['friends_group']][$value['friends_id']]['avatar'] = $value['header'];
            $friends_arr[$value['friends_group']][$value['friends_id']]['sign'] = $value['remark'];
        }
        //开始分组数据
        $group_list = $this->user_model->friends_group_list($uid);
        array_unshift($group_list, array('id' => 0, 'group_name' => '我的好友'));
        $group = array();
        foreach ($group_list as $key => $value) {
            $group[$key]['groupname'] = $value['group_name'];
            $group[$key]['id'] = '0';
            $group[$key]['online'] = (string) count($friends_arr[0]);
            $friends_list = isset($friends_arr[$value['id']]) ? $friends_arr[$value['id']] : array();
            $group[$key]['list'] = array_values($friends_list);
        }
        return json_encode($group);
    }

    /**
     * 群员列表
     */
    function group_list() {
        $group_id = $this->request->get('id');
        if (empty((int) $group_id)) {
            exit;
        }
        //群信息
        $group_info = $this->user_model->get_group_info($group_id);
        if (empty($group_info)) {
            $data['code'] = '-1';
            $data['msg'] = '不存在的群';
            $data['data'] = '';
            echo json_encode($data);
            exit;
        }
        $admin_info = $this->user_model->get_user_info($group_info['admin_id']);
        $owner['username'] = $admin_info['nick_name'];
        $owner['id'] = $admin_info['id'];
        $owner['avatar'] = $admin_info['header'];
        $owner['sign'] = $admin_info['remark'];
        //群员列表
        $user_list = $this->user_model->get_group_list($group_id);
        $group['code'] = 0;
        $group['msg'] = '';
        $group['data']['owner'] = $owner;
        $group['data']['members'] = count($user_list) ? count($user_list) : 0;
        $group['data']['list'] = $user_list;
        echo json_encode($group);
    }

}
